﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;
using Job.Logic.BaseContent;
using System.Collections.Generic;

namespace Job.Web.Person
{
    public partial class PersonLogin : BasePage
    {
        private int ErrorCode
        {
            get { return myR["error"].IntValue; }
        }

        private int action
        {
            get { return myR["action"].IntValue; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (action == 1)
            {
                Logoff();
            }
            if (ErrorCode == 1)
                message.InnerHtml = "<font color='#990000'>　未登录系统或长时间未操作，请重新登录！</font>";
            else if (ErrorCode == 2)
                message.InnerHtml = "<font color='#990000'>　系统内部错误，请重新登录！</font>";
            else
                message.InnerHtml = "根据相关人才流动法规条例规定，<br>企业在招聘过程中不得以押金、<br>报名费等任何形式收取求职者费用。";

            if (!this.IsPostBack)
            {
                BindHunterJob();
            }

        }

        private void Logoff()
        {
            MySession session = new MySession();
            if (session.GetPersonalFromSession() != null)
            {
                session.RemovePersonalFromSession();
            }
            string url = WebCommon.GetApplicationPath(Request) + "Person/PersonLogin.aspx?categoryid=1";
            Response.Redirect(url);
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            PersonalLogic logic = new PersonalLogic();
            JobPersonAccount account = logic.Login(txtLoginName.Text.Trim());
            if (null == account)
            {
                message.InnerHtml = "<font color='#990000'>　登录帐号不存在！</font>";
                return;
            }

            if (account.Password.Equals(txtPwd.Text.Trim()))
            {
                logic.UpdateLogin(account.PersonAccountId);
                SessionPersonal spersonal = new SessionPersonal()
                {
                    LoginName = txtLoginName.Text.Trim(),
                    PersonAccountid = account.PersonAccountId,
                    UserName = account.UserName
                };

                MySession session = new MySession();
                session.AddPersonal2Session(spersonal);

                Response.Redirect(WebCommon.GetApplicationPath(Request) + "Person/PersonManager.aspx?categoryid=1&ctrlid=2");
            }
            else
            {
                message.InnerHtml = "<font color='#990000'>　用户名或密码错误！</font>";
            }
        }

        //绑定猎头职位列表
        private void BindHunterJob()
        {
            HunterJobLogic jobLogic = new HunterJobLogic();
            DataTable dt = jobLogic.GetTopJob(26);
            if (null != dt)
            {
                int count = dt.Rows.Count;
                IList<JobRow> jobs = new List<JobRow>();
                int rows = count / 2;
                if ((count % 2) > 0) rows++;

                for(int i = 0 ; i < rows ; i++)
                {
                    JobRow jobRow = new JobRow();
                    jobRow.jobNo1 = dt.Rows[i * 2][1].ToString();
                    jobRow.jobName1 = (string)dt.Rows[i * 2][2].ToString();

                    if ((i * 2 + 1) < count)
                    {
                        jobRow.jobNo2 = (string)dt.Rows[i * 2 + 1][1].ToString();
                        jobRow.jobName2 = (string)dt.Rows[i * 2 + 1][2].ToString();
                    }
                    jobs.Add(jobRow);
                }

                jobList.DataSource = jobs;
                jobList.DataBind();                
            }
        }


        protected void jobList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink job1 = (HyperLink)e.Item.FindControl("job1");
                        Label lbJobNo1 = (Label)e.Item.FindControl("jobNo1");
                        if (job1.Text != null && job1.Text.Length > 0)
                        {
                            job1.NavigateUrl = "../Item.aspx?categoryid="
                            + Convert.ToString((int)Constants.CategoryType.Hunter) + "&subjectid="
                            + Convert.ToString((int)Job.Web.Framework.Constants.SubjectType.HunterPositionList)
                            + "&jobno=" + lbJobNo1.Text.Trim();
                        }


                        HyperLink job2 = (HyperLink)e.Item.FindControl("job2");
                        Label lbJobNo2 = (Label)e.Item.FindControl("jobNo2");
                        if (job2.Text != null && job2.Text.Length > 0)
                        {
                            job2.NavigateUrl = "../Item.aspx?categoryid="
                            + Convert.ToString((int)Job.Web.Framework.Constants.CategoryType.Hunter) + "&subjectid="
                            + Convert.ToString((int)Constants.SubjectType.HunterPositionList)
                            + "&jobno=" + lbJobNo2.Text.Trim();
                        }
                        break;
                    }

            }
        }
    }
}
